package com.charm.watercress.adapter

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import com.alibaba.android.arouter.launcher.ARouter
import com.charm.watercress.ui.fragment.BookFragment

/**
 * Created by tools on 2018/1/4.
 */
class ChannelAdapter(fm: FragmentManager, channelList: ArrayList<String>) : FragmentPagerAdapter(fm) {
    private var mChannelList: ArrayList<String> = channelList
    private var fragments: ArrayList<Fragment> = ArrayList()
    override fun getItem(position: Int): Fragment {
        when (position) {
            0 -> {
                if (fragments.size < position + 1) {
                    fragments.add(ARouter.getInstance().build("/watercress/film").navigation() as Fragment)
                }
            }
            1 -> {
                if (fragments.size < position + 1) {
                    fragments.add(ARouter.getInstance().build("/watercress/book").navigation() as Fragment)
                }
            }
        }
        return fragments[position]
    }

    override fun getCount(): Int {
        return if (mChannelList != null && mChannelList.isNotEmpty()) {
            mChannelList.size
        } else 0
    }

    override fun getPageTitle(position: Int): CharSequence {
        return if (mChannelList != null && position < mChannelList.size) {
            mChannelList[position]
        } else {
            position.toString() + ""
        }
    }
}